package com.ruoyi.web.controller.common;

import com.ruoyi.common.utils.FilesUpLoadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class FilesUploadController {

    private final FilesUpLoadUtil filesUploadUtil;

    public FilesUploadController(FilesUpLoadUtil filesUploadUtil) {
        this.filesUploadUtil = filesUploadUtil;
    }

    // 多图片上传接口
    @PostMapping("/images")
    public ResponseEntity<List<String>> uploadMultipleImages(@RequestParam("files") MultipartFile[] files) {
        try {
            List<String> uploadedUrls = filesUploadUtil.uploadMultipleFiles(files);
            System.out.println("uploadedUrls: " + uploadedUrls);
            return ResponseEntity.ok(uploadedUrls);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
//    @PostMapping("/image")
//    public ResponseEntity<String> uploadSingleImage(@RequestParam("file") MultipartFile file) {
//        System.out.println(file);
//        try {
//            if (file.isEmpty()) {
//                return ResponseEntity.badRequest().body("文件为空");
//            }
//
//            // 打印调试日志
//            System.out.println("文件名：" + file.getOriginalFilename());
//            System.out.println("文件大小：" + file.getSize());
//
//            // 上传逻辑
//            String objectName = filesUploadUtil.fileUploadDir + "/" + UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
//            String uploadedUrl = filesUploadUtil.uploadFileToOSS(file.getBytes(), objectName);
//
//            return ResponseEntity.ok(uploadedUrl);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败：" + e.getMessage());
//        }
//    }

    // 单图片上传接口
    @PostMapping("/image")
    public ResponseEntity<Map<String, Object>> uploadSingleImage(@RequestParam("file") MultipartFile file) {
        Map<String, Object> response = new HashMap<>();
        try {
            if (file.isEmpty()) {
                response.put("code", 400);
                response.put("message", "文件为空");
                return ResponseEntity.badRequest().body(response);
            }

            // 根据文件名生成唯一对象名
            String objectName = filesUploadUtil.fileUploadDir + "/"
                    + UUID.randomUUID().toString() + "_" + file.getOriginalFilename();

            // 调用工具类上传单个文件
            String uploadedUrl = filesUploadUtil.uploadFileToOSS(file.getBytes(), objectName);

            System.out.println("uploadedUrl: " + uploadedUrl);

            // 返回上传成功的响应
            response.put("code", 200);
            response.put("message", "上传成功");
            response.put("data", Map.of("url", uploadedUrl, "filename", file.getOriginalFilename()));
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            e.printStackTrace();
            response.put("code", 500);
            response.put("message", "文件上传失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
}
